package logic

import (
	"strconv"
	"time"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/models/orders"
	"yundian/internel/app/models/paysetting"
	"yundian/internel/app/models/placemodel"
	"yundian/internel/app/models/prices"
	"yundian/internel/pkg/dingo/global"
	"yundian/internel/pkg/utils"
	"yundian/internel/pkg/wechatpay"
)

// 订单金额Struct
type JSAPIAmount struct {
	Total    int64  `json:"total"`
	Currency string `json:"currency"`
}

// 支付者Struct
type Payer struct {
	Openid string `json:"openid"`
}

// 场景信息Struct
type SceneInfo struct {
	PayerClientIp string `json:"payer_client_ip"`
	DeviceId string `json:"device_id"`
	StoreInfo *StoreInfo `json:"store_info"`
}

// 门店信息
type StoreInfo struct {
	Id   string `json:"id"`
	Name string `json:"name"`
	AreaCode string `json:"area_code"`
	Address string `json:"address"`
}

/**
* JSAPI 下订单
 */
func (logic *OrderLogic) TransactionsPayApiParamV3(param *request.CreateOrderReq, wc *paysetting.PaySetting) *wechatpay.PayData {
	tradeNo := utils.Join(global.Const.Pre.OrderPre, time.Now().Format("20060102150405"), utils.GetRandomInt(6))
	placeInfo := dao.NewPlaceDao().GetPlaceById(param.PlaceId).Unwrap().(*placemodel.PlaceModel)

	// 获取支付金额
	ruleInfo := dao.NewPricesDao().GetPriceDetail(param.ProductId).Unwrap().(*prices.PriceModel)
	var money int64
	if param.FeeMode == 1 {
		money = ruleInfo.Deposit
	} else {
		money = ruleInfo.PayMoney
	}

	if money < 0 {
		money = 0
	}

	apiData := wechatpay.NewPayData()

	apiData.Set("appid", wc.AppId)
	apiData.Set("mchid", wc.MchId)
	apiData.Set("description", global.Const.Desc.JSAPIDescription)
	apiData.Set("out_trade_no", tradeNo)
	apiData.Set("attach", param.ProductId)
	apiData.Set("notify_url", wc.DepositNotifyUrlV3)

	amount := &JSAPIAmount{}
	amount.Total = money
	amount.Currency = "CNY"
	apiData.Set("amount", amount)
	payer := &Payer{}
	payer.Openid = param.Openid
	apiData.Set("payer", payer)

	storeInfo := &StoreInfo{}
	storeInfo.Id = strconv.Itoa(placeInfo.Id)
	storeInfo.Name = placeInfo.PlaceName
	storeInfo.AreaCode = ""
	storeInfo.Address = utils.Join(placeInfo.Address)
	sceneInfo := &SceneInfo{}
	sceneInfo.DeviceId = param.DeviceSn
	sceneInfo.PayerClientIp = param.ClientIp
	sceneInfo.StoreInfo = storeInfo
	apiData.Set("scene_info", sceneInfo)

	return apiData
}

/**
* 退款API构建
* @orderInfo 订单信息
* @transactionId 微信支付订单号
* @reason 退款理由
* @settMoney 退款金额
* @refundNotifyUrl 退款回调
 */
func (logic *OrderLogic) HandleRefundApiParamV3(orderInfo *orders.OrderModel, transactionId, reason string, settMoney int64, refundNotifyUrl string) *wechatpay.PayData {
	refundNo := utils.Join(global.Const.Pre.RefundPre, time.Now().Format("20060102150405"), utils.GetRandomInt(6)) // 退款订单号
	pd := wechatpay.NewPayData()
	pd.Set("transaction_id", transactionId)
	//pd.Set("out_trade_no", orderInfo.TradeNo)
	pd.Set("out_refund_no", refundNo)
	pd.Set("reason", reason)
	pd.Set("notify_url", refundNotifyUrl)

	amount := &Amount{}
	amount.Refund = settMoney
	amount.Total = orderInfo.Money
	amount.Currency = "CNY"

	pd.Set("amount", amount)  // 订单金额信息

	return pd
}
